﻿using CloudDcPc.Entity;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudDcPc.Bean
{
    public class CheckOutGzBean
    {
        private MySqlConnection myConn = null;
        private DBConnection dbc = null;
        private MySqlCommand cmd = null;
        public CheckOutGzBean()
        {
            this.dbc = new DBConnection();
            this.myConn = this.dbc.getConnection();
        }

        public CheckOutGz GetGz(string order_num)
        {
            string sql = "select * from dc_checkout_ext where checkout_num='C"+order_num+"'";
            CheckOutGz gz = null;
            try
            {
                if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
                cmd = new MySqlCommand(sql, myConn);
                MySqlDataReader reader = cmd.ExecuteReader();
                if(reader.Read()){
                gz = new CheckOutGz(){
                    Sign_name = (string)reader["sign_name"],
                    Department = (string)reader["department"]
                };
                }
            }finally
            {
                dbc.close();
            }
            return gz;
        }

        /// <summary>
        /// 添加一条结账信息(挂账)
        /// </summary>
        public bool AddCheckOutGz(List<CheckOut> checkOuts, List<CheckOutGz> checkOutGzs)
        {
            string sql = "";
            string sql_1 = "";
            string sql_2 = "";
            string sql_3 = "";

            if (myConn.State != System.Data.ConnectionState.Closed) System.Threading.Thread.Sleep(1000); myConn.Open();
            MySqlTransaction myTran = myConn.BeginTransaction();
            try
            {
                for (int i = 0; i < checkOuts.Count; i++)
                {
                    if (i != 0)
                    {
                        sql = "insert into dc_checkout_ext(checkout_num,tab_num,sign_name,department,phone,time,sum,doer,approver) values('" + checkOuts[i].Checkout_num + "','" + checkOuts[i].Tab_num + "','" + checkOutGzs[i].Sign_name + "','" + checkOutGzs[i].Department + "','" + checkOutGzs[i].Phone + "',NOW(),'0','" + checkOutGzs[i].Doer + "','" + checkOutGzs[i].Approver + "')";
                        sql_1 = "insert into dc_checkout(checkout_num,order_num,tab_num,food_sum,drink_sum,sum,final_cash,sub_sum,approver,sign_name,time,tgq_sum,pc_sum) values('" + checkOuts[i].Checkout_num + "','" + checkOuts[i].Order_num + "','" + checkOuts[i].Tab_num + "','0','0','0','0','0','" + checkOutGzs[i].Approver + "','" + checkOutGzs[i].Sign_name + "',NOW(),'" + checkOuts[i].Tgq_sum + "','" + checkOuts[i].Pc_sum + "')";
                        sql_2 = "update dc_tab set status=1 where tab_num='" + checkOuts[i].Tab_num + "'";
                        sql_3 = "update dc_order set haschecked=1 where order_num='" + checkOuts[i].Order_num + "'";
                    }
                    else
                    {
                        sql = "insert into dc_checkout_ext(checkout_num,tab_num,sign_name,department,phone,time,sum,doer,approver) values('" + checkOuts[i].Checkout_num + "','" + checkOuts[i].Tab_num + "','" + checkOutGzs[i].Sign_name + "','" + checkOutGzs[i].Department + "','" + checkOutGzs[i].Phone + "',NOW(),'" + (checkOuts[i].Sum - checkOuts[i].Sub_sum) + "','" + checkOutGzs[i].Doer + "','" + checkOutGzs[i].Approver + "')";
                        sql_1 = "insert into dc_checkout(checkout_num,order_num,tab_num,food_sum,drink_sum,sum,final_cash,sub_sum,cash_sum,djq_sum,xyk_sum,hyk_sum,approver,sign_name,time,tgq_sum,pc_sum) values('" + checkOuts[i].Checkout_num + "','" + checkOuts[i].Order_num + "','" + checkOuts[i].Tab_num + "','" + checkOuts[i].Food_sum + "','" + checkOuts[i].Drink_sum + "','" + checkOuts[i].Sum + "','" + checkOuts[i].Final_cash + "','" + checkOuts[i].Sub_sum + "','" + checkOuts[i].Cash_sum + "','" + checkOuts[i].Djq_sum + "','" + checkOuts[i].Xyk_sum + "','" + checkOuts[i].Hyk_sum + "','" + checkOutGzs[i].Approver + "','" + checkOutGzs[i].Sign_name + "',NOW(),'" + checkOuts[i].Tgq_sum + "','" + checkOuts[i].Pc_sum + "')";
                        sql_2 = "update dc_tab set status=1 where tab_num='" + checkOuts[i].Tab_num + "'";
                        sql_3 = "update dc_order set haschecked=1 where order_num='" + checkOuts[i].Order_num + "'";
                    }


                    cmd = new MySqlCommand(sql, myConn);
                    cmd.ExecuteNonQuery();

                    cmd = new MySqlCommand(sql_1, myConn);
                    cmd.ExecuteNonQuery();

                    cmd = new MySqlCommand(sql_2, myConn);
                    cmd.ExecuteNonQuery();

                    cmd = new MySqlCommand(sql_3, myConn);
                    cmd.ExecuteNonQuery();


                    string sql_cp1 = "insert into dc_hisorder (select * from dc_order where order_num='" + checkOuts[i].Order_num + "')";
                    string sql_rm1 = "delete from dc_order where order_num='" + checkOuts[i].Order_num + "'";
                    string sql_cp2 = "insert into dc_hisorders (select * from dc_orders where order_num='" + checkOuts[i].Order_num + "')";
                    string sql_rm2 = "delete from dc_orders where order_num='" + checkOuts[i].Order_num + "'";
                    string sql_cp3 = "insert into dc_hisreturned (select * from dc_returned where order_num='" + checkOuts[i].Order_num + "')";
                    string sql_rm3 = "delete from dc_returned where order_num='" + checkOuts[i].Order_num + "'";
                    cmd = new MySqlCommand(sql_cp1, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_rm1, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_cp2, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_rm2, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_cp3, myConn);
                    cmd.ExecuteNonQuery();
                    cmd = new MySqlCommand(sql_rm3, myConn);
                    cmd.ExecuteNonQuery();
                }
                myTran.Commit();
                return true;
            }
            catch
            {
                myTran.Rollback();
                return false;
            }
            finally
            {
                dbc.close();
            }
        }
    }
}
